/**
 * Copyright (C), 2020, JieYi Software System Co., Ltd.
 * All rights reserved.
 * FileName:       ExcuteAndShowController.java
 *
 * @Description:
 * @author: feiwe
 * Modification History:
 * Date         Author        Version        Discription
 * -------------------------------------------------------
 * 2020/11/9      feiwe                         add
 */


package com.fengwei.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fengwei.executor.MysqlExecutor;
import com.fengwei.form.MapForObject.MapForObject;
import com.fengwei.form.MdmsUser;
import com.fengwei.requestdata.ExcuteAndShowAddCommonSqlData;
import com.fengwei.requestdata.ExcuteAndShowDelCommonSqlData;
import com.fengwei.requestdata.ExcuteAndShowEditCommonSqlData;
import com.fengwei.requestdata.ExcuteAndShowExecSqlData;
import com.fengwei.requestdata.ExcuteAndShowGetCommonSqlData;
import com.fengwei.requestdata.ExcuteAndShowPagingListData;
import com.fengwei.service.ConsoleCommonService;
import com.fengwei.service.ExcuteAndShowService;
import com.fengwei.util.PltResult;
import com.fengwei.util.ResponseUtil;
import com.fengwei.verify.Verify;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
public class ExcuteAndShowController {
    private final static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private MysqlExecutor mysqlExecutor;

    @Autowired
    private ConsoleCommonService consoleCommonService;

    @Autowired
    private ExcuteAndShowService excuteAndShowService;

    @GetMapping(value = "/excuteAndShow/loadPage")
    public String loadPage(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        return "excuteAndShow";
    }

    /**
     * 执行SQL
     */
    @PostMapping(path = "/excuteAndShow/execSql")
    @ResponseBody
    public Map<String, Object> execSql(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);

        ExcuteAndShowExecSqlData excuteAndShowExecSqlData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<ExcuteAndShowExecSqlData>() {
                });

        // 校验传入参数
        Verify.verifyData(excuteAndShowExecSqlData);

        // 正式处理业务
        Map<String, Object> data = excuteAndShowService.execSql(excuteAndShowExecSqlData, request, mdmsUser);
        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 执行SQL完SQL之后的分页
     */
    @PostMapping(path = "/excuteAndShow/pagingList")
    @ResponseBody
    public Map<String, Object> pagingList(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);

        ExcuteAndShowPagingListData excuteAndShowExecSqlData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<>() {
        });

        // 校验传入参数
        Verify.verifyData(excuteAndShowExecSqlData);

        // 正式处理业务
        Map<String, Object> data = excuteAndShowService.pagingList(excuteAndShowExecSqlData, request, mdmsUser);
        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 执行SQL完SQL之后的分页
     */
    @PostMapping(path = "/excuteAndShow/getCommonSql")
    @ResponseBody
    public Map<String, Object> getCommonSql(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);

        ExcuteAndShowGetCommonSqlData excuteAndShowGetCommonSqlData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<>() {
        });

        // 校验传入参数
        Verify.verifyData(excuteAndShowGetCommonSqlData);

        // 正式处理业务
        Map<String, Object> data = excuteAndShowService.getCommonSql(excuteAndShowGetCommonSqlData, request, mdmsUser);
        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 执行SQL完SQL之后的分页
     */
    @PostMapping(path = "/excuteAndShow/addCommonSql")
    @ResponseBody
    public Map<String, Object> addCommonSql(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);

        ExcuteAndShowAddCommonSqlData excuteAndShowAddCommonSqlData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<>() {
                });

        // 校验传入参数
        Verify.verifyData(excuteAndShowAddCommonSqlData);

        // 正式处理业务
        Map<String, Object> data = excuteAndShowService.addCommonSql(excuteAndShowAddCommonSqlData, request, mdmsUser);
        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 执行SQL完SQL之后的分页
     */
    @PostMapping(path = "/excuteAndShow/editCommonSql")
    @ResponseBody
    public Map<String, Object> editCommonSql(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);

        ExcuteAndShowEditCommonSqlData excuteAndShowEditCommonSqlData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<>() {
                });

        // 校验传入参数
        Verify.verifyData(excuteAndShowEditCommonSqlData);

        // 正式处理业务
        Map<String, Object> data = excuteAndShowService.editCommonSql(excuteAndShowEditCommonSqlData, request, mdmsUser);
        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 执行SQL完SQL之后的分页
     */
    @PostMapping(path = "/excuteAndShow/delCommonSql")
    @ResponseBody
    public Map<String, Object> delCommonSql(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);

        ExcuteAndShowDelCommonSqlData excuteAndShowDelCommonSqlData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<>() {
                });

        // 校验传入参数
        Verify.verifyData(excuteAndShowDelCommonSqlData);

        // 正式处理业务
        Map<String, Object> data = excuteAndShowService.delCommonSql(excuteAndShowDelCommonSqlData, request, mdmsUser);
        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

}
